@extends('admin/layout')

@section('content')
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
                <li class="active">兑换规则</li>
            </ol>

        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-md-12" style="width: 100%;">
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">内容</h3>
                        </div>
                        <span id="msg" style="display: none">
                            @if (count($errors) > 0)
                                @foreach ($errors->all() as $error)
                                    <div style="width: 100%;">
                                        <span style="color: red;display: block;margin: 0px auto;width: 200px;text-align: center;">{{ $error }}</span>
                                    </div>
                                @endforeach
                            @else
                                <div style="width: 100%;">
                                    <span style="color: green;display: block;margin: 0px auto;width: 200px;text-align: center;">{{ Session::get('msg') }}</span>
                                </div>
                            @endif
                        </span>
                        <script type="text/javascript">
                            var _msg = $('#msg div span').text();
                            console.log(_msg);
                            if(_msg != ''){
                                layer.msg(_msg);
                            }
                        </script>
                        <div class="box-body">
                            <form  action="{{ url('admin/rule/update') }}" method="post">
                                {{ csrf_field() }}
                                <input type="hidden" name="id" value="{{ $rule->id }}">
                                <div class="box-body" style="width: 50%;">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">标题</label>
                                        <input type="text" name="name" class="form-control" value="{{ $rule->name }}" placeholder="name">
                                    </div>
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">内容</label>
                                        <textarea id="editor_id" rows="4" style="width: 100%;height: 600px;" name="content">{{ $rule->content }}</textarea>
                                    </div>
                                </div>
                                <div class="box-footer">
                                    <button type="submit" class="btn btn-primary">提交</button>
                                </div>
                            </form>
                        </div>
                        <!-- /.box -->
                    </div>
                    <!-- /.row -->
                </div>
            </div>
        </section>
        <!-- /.content -->
    </div>

    <script>
        $(function() {
            var editor;
//            KindEditor.ready(function(K) {
//                editor = K.create('textarea[name="content"]', {
//                    afterBlur: function () { this.sync(); },
//                    resizeType : 1,
//                    allowPreviewEmoticons : false,
//                    allowImageUpload : false,
////                    syncType:form,
//                    items : [
//                        'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
//                        'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
//                        'insertunorderedlist', '|', 'emoticons','image','link']
//                });
//
//
//            });
            KindEditor.ready(function(K) {
                window.editor = K.create('textarea[name="content"]',{
                    afterBlur: function () { this.sync(); }
                });

            });
            $("form").submit(function(event) {
                event.preventDefault();
                var url = $(this).attr("action");
                var data = $("form").serialize();
                console.log(data);
                $.ajax({
                    url: url,
                    type: "POST",
                    data: data,
                    dataType: "json",
                    success: function(res) {
                        console.log(res);
                        if(res.code == 100) {
                            window.location.href = "/admin/rule";
                            layer.msg(res.msg)
                        }
                        else {
                            layer.msg(res.msg)
                        }
                    }
                });
            });
        });
    </script>
@stop